<?
$starta = microtime_float();

$info = $GLOBALS[info];
$min_year = getMinYear($GLOBALS['id']);
if(!is_numeric($min_year)){
	$min_year = date('Y');
}

$num_years = date('Y') - $min_year + 1;

$stores = getStores($GLOBALS['id']);

//print_r($info);
//print_r($GLOBALS['currencies']);
//$params[sale_]
//$options[group_by] = array();
//getGeneralResults($params, $options);


$params = array();
$options = array();
if($info['payment_currency']){ $params['payment_currency'] = $info['payment_currency']; }
if($info['store_id']){ $params['store_id'] = $info['store_id']; }
if($info['year'] || $info['month']){ $for_date = '%'.$info['year'].'-'.$info['month']; }

$period_year = $info['year'] ? $info['year'] : 'Any Year';
$period_month = $info['month'] ? date('F', mktime(0, 0, 0, $info['month'], 1, 1)) : 'All Months';

if($info['type'] == 'invoice'){
	if($for_date){
		$params['order_date__'] = $for_date;
	}
	$options['group_by'] = array('count', 'payment_currency', 'sum_order_amount_total');
	$params['wholesale_order'] = 't';
	$inv_wh = getByCurr('inv', $params, $options);
	$params['wholesale_order'] = 'f';
	$inv_t = getByCurr('inv', $params, $options);
}elseif($info['type'] == 'credit_note'){
        if($for_date){
                $params['sale_date__'] = $for_date;
        }
	$options['group_by'] = array('count', 'payment_currency', 'sum_amount_total');
	$params['wholesale_order'] = 't';
	$cn_wh = getByCurr('cn', $params, $options);
	$params['wholesale_order'] = 'f';
	$cn_t =  getByCurr('cn', $params, $options);
}else{
        if($for_date){
                $params['order_date__'] = $for_date;
        }	
	$options['group_by'] = array('count', 'payment_currency', 'sum_order_amount_total');
	$params['wholesale_order'] = 't';
	$inv_wh = getByCurr('inv', $params, $options);
	$params['wholesale_order'] = 'f';
	$inv_t = getByCurr('inv', $params, $options);

        if($for_date){
		unset($params['order_date__']);
                $params['sale_date__'] = $for_date;
        }	
	$options['group_by'] = array('count', 'payment_currency', 'sum_amount_total');
	$params['wholesale_order'] = 't';
	$cn_wh = getByCurr('cn', $params, $options);
	$params['wholesale_order'] = 'f';
	$cn_t =  getByCurr('cn', $params, $options);	
}

//if(me()){
//echo '<pre>';
//echo '<br>inv_t<br>';
//print_r($inv_t);
//echo '<br>inv_wh<br>';
//print_r($inv_wh);
//echo '<br>cn_t<br>';
//print_r($cn_t);
//echo '<br>cn_wh<br>';
//print_r($cn_wh);
//echo '</pre>';
//}

$invoices_table = '';
$credit_notes_table = '';
$all_inv_count_t = 0;
$all_inv_count_wh = 0;
$all_cn_count_t = 0;
$all_cn_count_wh = 0;

foreach($GLOBALS['currencies'] as $k => $v){
	if(!$params['payment_currency'] or $params['payment_currency'] == $k){

	$inv_count_t = $inv_t[$k]['count'] ? $inv_t[$k]['count'] : 0;
	$inv_count_wh = $inv_wh[$k]['count'] ? $inv_wh[$k]['count'] : 0;
	$cn_count_t = $cn_t[$k]['count'] ? $cn_t[$k]['count'] : 0;
	$cn_count_wh = $cn_wh[$k]['count'] ? $cn_wh[$k]['count'] : 0;

	$all_inv_count_t = $all_inv_count_t + $inv_count_t;
	$all_inv_count_wh = $all_inv_count_wh + $inv_count_wh;
	$all_cn_count_t = $all_cn_count_t + $cn_count_t;
	$all_cn_count_wh = $all_cn_count_wh + $cn_count_wh;

	$inv_sum_t = $inv_t[$k]['sum_order_amount_total'] ? $inv_t[$k]['sum_order_amount_total'] : 0;
	$inv_sum_wh = $inv_wh[$k]['sum_order_amount_total'] ? $inv_wh[$k]['sum_order_amount_total'] : 0;
	$cn_sum_t = $cn_t[$k]['sum_amount_total'] ? $cn_t[$k]['sum_amount_total'] : 0;
	$cn_sum_wh = $cn_wh[$k]['sum_amount_total'] ? $cn_wh[$k]['sum_amount_total'] : 0;

	$inv_sum = $inv_sum_t + $inv_sum_wh;
	$cn_sum = $cn_sum_t + $cn_sum_wh;

	$inv_sum_av_t = $inv_count_t ? bcdiv($inv_sum_t, $inv_count_t, 2) : 0;
	$inv_sum_av_wh = $inv_count_wh ? bcdiv($inv_sum_wh, $inv_count_wh, 2) : 0;
	$cn_sum_av_t = $cn_count_t ? bcdiv($cn_sum_t, $cn_count_t, 2) : 0;
	$cn_sum_av_wh = $cn_count_wh ? bcdiv($cn_sum_wh, $cn_count_wh, 2) : 0;

	$inv_sum_t = number_format($inv_sum_t, 2, ',', ' ');
	$inv_sum_wh = number_format($inv_sum_wh, 2, ',', ' ');
	$cn_sum_t = number_format($cn_sum_t, 2, ',', ' ');
	$cn_sum_wh = number_format($cn_sum_wh, 2, ',', ' ');
	$inv_sum = number_format($inv_sum, 2, ',', ' ');
	$cn_sum = number_format($cn_sum, 2, ',', ' ');
	$inv_sum_av_t = number_format($inv_sum_av_t, 2, ',', ' ');
	$inv_sum_av_wh = number_format($inv_sum_av_wh, 2, ',', ' ');
	$cn_sum_av_t = number_format($cn_sum_av_t, 2, ',', ' ');
	$cn_sum_av_wh = number_format($cn_sum_av_wh, 2, ',', ' ');

	$invoices_table .= "<tr>
                <td align='center'>$inv_count_t</td>
                <td align='right' style='padding-right:20px;'>$v $inv_sum_t</td>
                <td align='right' style='padding-right:20px;'>$v $inv_sum_av_t</td>
                <td align='center'>$inv_count_wh</td>
                <td align='right' style='padding-right:20px;'>$v $inv_sum_wh</td>
                <td align='right' style='padding-right:20px;'>$v $inv_sum_av_wh</td>
                <td align='right' style='padding-right:20px;'>$v $inv_sum</td>
        </tr>";

	$credit_notes_table .= "<tr>
                <td align='center'>$cn_count_t</td>
                <td align='right' style='padding-right:20px;'>$v $cn_sum_t</td>
                <td align='right' style='padding-right:20px;'>$v $cn_sum_av_t</td>
                <td align='center'>$cn_count_wh</td>
                <td align='right' style='padding-right:20px;'>$v $cn_sum_wh</td>
                <td align='right' style='padding-right:20px;'>$v $cn_sum_av_wh</td>
                <td align='right' style='padding-right:20px;'>$v $cn_sum</td>
        </tr>";
	}
}

$enda = microtime_float();
//echo "<br>Executed in ".($enda - $starta)." seconds<br>";
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
	<td align="left">
	  <p><strong>A View from a Different Angle</strong><br>
	  The aim of the displayed results here is to give you a notion about the type, quantity, the total and the average value of the Invoices and Credit Notes issued by LiquiqidNet Ltd.:<br>
	  </p>
	  <blockquote>
		<strong>a.</strong> From your StoreName to your clients (Retail)<br>
		<strong>b.</strong> From ResellersPanel to you (Wholesale)
	</blockquote></td>
	<td align="right"></td>
  </tr>
</table>
<form name="generalSearch" id="generalSearch" onSubmit="return false;">
<table class="reg_table" width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
	<td>Month:<br />
	  <select name="info[month]">
		<option value="" <? if($info['month'] == '') {echo 'selected';} ?>>All Months</option>
		<option value="01" <? if($info['month'] == '01') {echo 'selected';} ?>>January</option>
		<option value="02" <? if($info['month'] == '02') {echo 'selected';} ?>>February</option>
		<option value="03" <? if($info['month'] == '03') {echo 'selected';} ?>>March</option>
		<option value="04" <? if($info['month'] == '04') {echo 'selected';} ?>>April</option>
		<option value="05" <? if($info['month'] == '05') {echo 'selected';} ?>>May</option>
		<option value="06" <? if($info['month'] == '06') {echo 'selected';} ?>>June</option>
		<option value="07" <? if($info['month'] == '07') {echo 'selected';} ?>>July</option>
		<option value="08" <? if($info['month'] == '08') {echo 'selected';} ?>>August</option>
		<option value="09" <? if($info['month'] == '09') {echo 'selected';} ?>>September</option>
		<option value="10" <? if($info['month'] == '10') {echo 'selected';} ?>>October</option>
		<option value="11" <? if($info['month'] == '11') {echo 'selected';} ?>>November</option>
		<option value="12" <? if($info['month'] == '12') {echo 'selected';} ?>>December</option>
	  </select></td>
	<td>Year:<br />
	  <select name="info[year]">
		<option value="">Any</option>
		<?
		for($i = 0; $i < $num_years; $i++){
			$year = date("Y", mktime(0, 0, 0, date('m'), 1, date('Y') - $i));
			$selected = $year == $info['year'] ? 'selected' : '';
			echo "<option value='$year' $selected>$year</option>";
		}
		?>
	  </select></td>
	<td>Currency:<br />
	  <select name="info[payment_currency]" id='currencies'>
		<option selected value="">All</option>
		<?
		foreach ($GLOBALS['currencies'] as $k => $v){
			$selected = $k == $info['payment_currency'] ? 'selected' : '';
			echo "<option value='$k' $selected>$k</option>";
		}
		?>
	  </select></td>
	<td>Type:<br />
	  <select name="info[type]">
		<option value="">All Types</option>
		<option value="invoice" <? if($info['type'] == 'invoice') {echo 'selected';} ?>>Invoice</option>
		<option value="credit_note" <? if($info['type'] == 'credit_note') {echo 'selected';} ?>>Credit Note</option>
	  </select></td>
	<td>Store:<br />
	  <select name="info[store_id]">
		<option value="">All Stores</option>
		<?
		foreach($stores as $k => $v){
			$selected = $v['store_id'] == $info['store_id'] ? 'selected' : '';
			echo "<option value='$v[store_id]' $selected>$v[title]</option>";
		}
		?>
	  </select></td>
	<td>
		<br />
		<input type='hidden' name='stepPage' value='general2.html'>
		<input name="submit" type="submit" value="&nbsp View &nbsp" onClick="sgeneral(getFormData('generalSearch', 'params'));">
	</td>
  </tr>
</table>
</form>
<?
if($inv_t or $inv_wh){
?>
<br />
<table class='bTable' cellspacing=0 cellpadding=0 width=100%>
	<tr>
		<td style="background:url(images/bkg_invoice_theader.gif) top repeat-x;" colspan="7" align="center"><strong>INVOICES for <?=$period_month?> <?=$period_year?></strong></td>
  	</tr>
	  <tr>
		<th colspan="3">Retail - <?=$all_inv_count_t;?></th>
		<th colspan="3">Wholesale - <?=$all_inv_count_wh;?></th>
		<th>Balance</th>
	  </tr>
	  <tr id="white_border_01">
		<td align="center"><strong>Count</strong></td>
		<td align="center"><strong>Total</strong></td>
		<td align="center"><strong>Average</strong></td>
		<td align="center"><strong>Count</strong></td>
		<td align="center"><strong>Total</strong></td>
		<td align="center"><strong>Average</strong></td>
		<td align="center"><strong>-</strong></td>
	  </tr>
<?=$invoices_table;?>
</table>
<?
}
?>
<?
if($cn_t or $cn_wh){
?>
<br />
<table class='bTable' cellspacing=0 cellpadding=0 width=100%>
	<tr>
		<td style="background:url(images/bkg_creditnote_theader.gif) top repeat-x;" colspan="7" align="center"><strong>CREDIT NOTES for <?=$period_month?> <?=$period_year?></strong></td>
  </tr>
	  <tr>
		<th colspan="3">Retail - <?=$all_cn_count_t;?></th>
		<th colspan="3">Wholesale - <?=$all_cn_count_wh;?></th>
		<th>Balance</th>
	  </tr>
	  <tr id="white_border_01">
		<td align="center"><strong>Count</strong></td>
		<td align="center"><strong>Total</strong></td>
		<td align="center"><strong>Average</strong></td>
		<td align="center"><strong>Count</strong></td>
		<td align="center"><strong>Total</strong></td>
		<td align="center"><strong>Average</strong></td>
		<td align="center"><strong>-</strong></td>
	  </tr>
<?=$credit_notes_table;?>
</table>
<?
}else{
	include($GLOBALS['path'].'no_results.html');
}
?>
<?
/*
<br />
<table class='bTable' cellspacing=0 cellpadding=0 width=100%>
	<tr>
		<td style="background:url(images/bkg_sbti_theader.gif) top repeat-x;" colspan="4" align="center"><strong>SELF-BILLING TaX INVOICES</strong></td>
  </tr>
	  <tr>
		<th>Invoice#</th>
		<th>Amount</th>
		<th>Payment Method</th>
		<th>Payment Status</th>
	  </tr>
	  <tr>
		<td align="center"><img src="images/ico_coins_sbti.gif" width="10" height="10"> <a href="#">1-3-1-106709-s</a></td>
		<td align="right" style="padding-right:20px;">US$48,00</td>
		<td align="center"><a href="#">PayPal</a></td>
		<td align="right" style="padding-right:20px;">upcoming</td>
	  </tr>
	  <tr>
		<td align="center"><img src="images/ico_coins_sbti.gif" width="10" height="10"> <a href="#">1-3-1-106709-s</a></td>
		<td align="right" style="padding-right:20px;">?48,00</td>
		<td align="center"><a href="#">Check</a></td>
		<td align="right" style="padding-right:20px;">upcoming [08/04/2006]</td>
	  </tr>
	  <tr>
		<td align="center"><img src="images/ico_coins_sbti.gif" width="10" height="10"> <a href="#">1-3-1-106709-s</a></td>
		<td align="right" style="padding-right:20px;">€ 48,00</td>
		<td align="center"><a href="#">Worldpay</a></td>
		<td align="right" style="padding-right:20px;">upcoming [08/04/2006]</td>
	  </tr>
	  <tr>
		<td align="center"><img src="images/ico_coins_sbti.gif" width="10" height="10"> <a href="#">1-3-1-106709-s</a></td>
		<td align="right" style="padding-right:20px;">US$48,00</td>
		<td align="center"><a href="#">Worldpay</a></td>
		<td align="right" style="padding-right:20px;">upcoming [08/04/2006]</td>
	</tr>
	  <tr>
		<td align="center"><img src="images/ico_coins_sbti.gif" width="10" height="10"> <a href="#">1-3-1-106709-s</a></td>
		<td align="right" style="padding-right:20px;">US$48,00</td>
		<td align="center"><a href="#">PayPal</a></td>
		<td align="right" style="padding-right:20px;">upcoming [08/04/2006]</td>
	  </tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
	<td align="right"><strong>JAN'05 NET PROFIT:</strong></td>
	<td width="20%" align="center"><b style="font-size:14px; color:#006600;">US$ 390,00</b></td>
  </tr>
</table>
*/
?>